<?php

namespace IDock\Insurance\TPBX\Request;

use Exception;
use IDock\Insurance\TPBX\TPBXEnums;
use IDock\Insurance\TPBX\Request\AllTPBXRequest;
use IDock\Insurance\TPBX\Response\UnderwritingResponse;
use IDock\Printer\NonCar\NonCarSubCoveragePrinter;
use IDock\Visitor\NonCar\NonCarProductVisitor;
use IDock\Visitor\Premium\PremiumInquiryVisitor;
use IDock\Visitor\SpecialClausesVisitor;

class UnderwritingRequest extends AllTPBXRequest
{
    public $requestType = 'V050';

    public $route = 'webapi/v1/apply/application';

    protected $response = UnderwritingResponse::class;

    protected $urlTip = '提交核保';

    /*** @var  PremiumInquiryVisitor $visitor */
    protected $visitor;

    public $attachmentList = [];

    public function generate(): array
    {
        $this->setConfig($this->visitor->getExtInfoVisitor()->argc);
        $arr = [
            'payment' => [
                'paymentPersonCode' => $this->visitor->getApplicantVisitor()->name,
                'paymentMode' => 22,
            ],
            'basePart' => $this->getBaseData(),
            'billInfo' => $this->getBillInfo(),
            'bizApply' => [
                'applyPolicyNo' => $this->visitor->getExtInfoVisitor()->argc['biApplyNo'] ?? ''
            ],
            'tpfApply' => [
                'applyPolicyNo' => $this->visitor->getExtInfoVisitor()->argc['ciApplyNo'] ?? ''
            ],
            'attachmentList' => $this->getAttachmentList()
        ];
        if($this->visitor->getNonCarUnderwritingVisitor()->getNonCarList()){
            //非车
            $arr['planCodeList'] = $this->getNonCarList();
        }
        return $arr;
    }

    private function getNonCarList()
    {
        $list = [];
        $nonCar = $this->visitor->getNonCarUnderwritingVisitor()->getNonCarList();
        foreach ($nonCar as $value) {
            /*** @var $value NonCarProductVisitor */
            $tmp = [
                'asAppliWithInsRelaship' => $this->visitor->getApplicantVisitor()->name == $this->visitor->getInsuredVisitor()->name ? '1' :'5',
                "coverageName" => $value->argc['coverageName'] ?? '',
                "productCode" => $value->productCode,
                "effectiveDate" => date('Ymd',strtotime($this->visitor->biStartData)),
                "expireDate" => date('Ymd',strtotime($this->visitor->biEndData)),
                "planCode" => $value->planCode,
                "productName" => $value->argc['productName'] ?? '',
                'nonCoverageList' => []
            ];
            foreach ($value->subCoverage as $item){
                /*** @var NonCarSubCoveragePrinter $item */
                $tmp['nonCoverageList'][] = [
                    'coverageCode' => $item->subCode,
                    'coverageName' => $item->subName,
                    'insuredAmount' => $item->amount,
                ];
            }
            $list[] = $tmp;
        }
        return $list;
    }

    private function getAttachmentList()
    {
        $list = [];
        foreach ($this->attachmentList as $value){
            $list[] = [
                'attachmentNo' => $value['number'],
                'attachmentName' => basename($value['url']),
                'uploadFileType' => TPBXEnums::$uploadImageType[$value['type'] ?? ''] ?? '',
            ];
        }
        return $list;
    }

    private function getBaseData()
    {
        $arr = [
            'applicationFormNo' => 'HXY'.date('YmdHis').mt_rand(10000,99999),
            'isMobileDevices' => '0',//是否移动设备
            'tpCalculateNo' => $this->visitor->getUnderwritingVisitor()->inquiryNo,
            'disputedSettleMode' => '0',
            'standarded' => '0',
            'ipAddress' => '',
            'macAddress' => '',
            'waiterSpecialMarkKey' => 'Key0000BXT',
        ];
        return $arr;
    }

    private function getBillInfo()
    {
        $arr = [
            'billTaxPayerType' => $this->visitor->getApplicantVisitor()->typeCode == 'R1' ? '0' : '1',// 0境内个人
            'billType' => '',
            'billPersonType' => $this->visitor->getApplicantVisitor()->typeCode == 'R1' ? '1' : '0',
            'billEmail' => '',
            'billCertType' => '',
            'billCertNo' => '',
            'billPersonName' => '',
            'billMobilePhone' => '',
            'isBusinessBill' => '',
        ];
        return $arr;
    }

    /**
     * 通过身份证获取性别
     * @param $idcard
     * @return int
     * @throws Exception
     */
    private function getGenderByIDcard($idCard){
        if(strlen($idCard) != 18){
            throw new Exception("身份证位数不合法");
        }
        $gender = intval(substr($idCard,16,1)) % 2;
        return $gender ? '2' : '1';
    }

    private function getBirthday($idCard)
    {
        if(strlen($idCard) != 18){
            throw new Exception("身份证位数不合法");
        }
        return substr($idCard,6,4).'-'.substr($idCard,10,2).'-'.substr($idCard,12,2);
    }


}
